from hep.aida import *

from java.awt import Color
from java.awt import Font
from java.lang import *
from java.util import *
from java.io import *
from java.text import DecimalFormat


from jhplot.jadraw import *
from jhplot  import *

from fr.in2p3.ipnl.aidautilities import *

""" jscinttools libraries """
from jscinttools.datafit import DataFitter
from jscinttools.datafit import FitSettings
from jscinttools.datafit import FitFuncFactory
from jscinttools.scint import SetupFileReader
from jscinttools.utilities.scintutilities import ScintUtil
from jscinttools.utilities.aidautilities import AidaFileReader
from jscinttools.utilities.aidautilities import AUtil
from jscinttools.utilities.aidautilities import AidaFactories
from jscinttools.utilities.plotterutilities import BeautifierFactory
from jscinttools.utilities.plotterutilities import PlotFonts
from jscinttools.utilities.plotterutilities import PlotColors
from jscinttools.utilities.plotterutilities import FileExtensions

Locale.setDefault(Locale.ENGLISH)
ff = FitFuncFactory.pulseFuncFactory()

showFigures = True
writeFigures = not showFigures


""" Define the Path and files names"""
aidaPath = "/home/marc-antoine/Documents/ZnWO4/Run_12/Reduced/TDC/2011_08_15/"
aidaFile = "Am241_4K_1201_PhotonReanalysis_NoTuples"
outPath = aidaPath
#startPar = [0.32, 0.63, 0.12, 16, 0.15, 121, 0.04, 315, 0.001]
startPar = [0.01, 0.01, 0.32, 0.63, 0.12, 16, 0.15, 121, 0.04, 315, 0.001]
fitFunc = ff.nExpBck()
win = [0, 1600, 0.001, 1]
winZoom = [0, 0.5, 0.1, 1]
title = "Average Pulse in BGO with ^{241}Am at 4 K"
preliminary = False

outputFile = aidaFile.split("_PhotonReanalysis_NoTuples")[0]
#avEvtPath = "Reanalysis/Average pulse shapes/Variable binning/Weighted Average Pulse from First Photon on Ch2, Ch1, All Cuts"
avEvtPath = "Reanalysis/Average pulse shapes/Variable binning/Special Bin, Average Pulse from First Photon on Ch2, Ch1, All Cuts"

afr = AidaFileReader(aidaPath, aidaFile)
avEvtHist = afr.grabHisto1D(avEvtPath)
avEvtHist.scale(1.0 / avEvtHist.maxBinHeight())

df = DataFitter(fitFunc)
fs = FitSettings(startPar)

prelim = JaText("PRELIMINARY", 0.05, 0.35, "NDC")
prelim.setFont(PlotFonts.SF_B_30)
prelim.setColor(Color.RED)
prelim.setRotAngle(-45)

"""
###############################################################
# Plotting Part
###############################################################
"""
""" Create the plottable JHep objects from AIDA IObjects"""

	
avEvt = H1D(avEvtHist)	
avEvt.setTitle("BGO Pulse with ^{241}Am at T = 4 K")
avEvt.setColor(Color.BLUE)
avEvt.setStyle("h")
avEvt.setPenWidth(2)
avEvt.setErrAll(False)
		
fitRes = df.doFit(avEvtHist, fs)
print(AidaUtil.printResultsOfFit(fitRes))

iFunc = fitRes.fittedFunction()

func = F1D(iFunc)
func.setTitle(avEvt.getTitle())
func.setMin(10000)
func.setColor(Color.RED)
func.setPenWidth(2)
func.setMin(win[0])
func.setMax(win[1])

funcZoom = F1D(iFunc)
funcZoom.setTitle(avEvt.getTitle())
funcZoom.setMin(10000)
funcZoom.setColor(Color.RED)
funcZoom.setPenWidth(2)
funcZoom.setMin(winZoom[0])
funcZoom.setMax(winZoom[1])

title = avEvt.getTitle()
plotter = HPlotJa(title, 600, 600, 2, 1, True)
plotter.setGTitle(title, PlotFonts.TITLE_FONT)
plotter.setAntiAlias(False)
plotter.visible(showFigures)

plotter.cd(1,1)	
plotter.setPad(1, 1, 0.125, 0.125, 0.75, 0.75)
plotter.setRange(win[0], win[1], win[2], win[3])
plotter.setLogScale(0, False)
plotter.setLogScale(1, True)
plotter.setTicFont(PlotFonts.TICK_FONT)
plotter.setShowStatBox(False)
plotter.showKey(False)
plotter.setNameX("Time (&micro;s)")
plotter.setNameY("Photons / Time Unit")

plotter.cd(2,1)	
plotter.setPad(2, 1, 0.525, 0.17, 0.3, 0.3)
plotter.setRange(winZoom[0], winZoom[1], winZoom[2], winZoom[3])
plotter.setLogScale(0, False)
plotter.setLogScale(1, True)
plotter.setTicFont(PlotFonts.TICK_FONT)
plotter.setShowStatBox(False)
plotter.showKey(False)
plotter.setNameX("")
plotter.setNameY("")

""" Plots all the JHep objects"""
plotter.cd(1,1)
plotter.draw(avEvt)
plotter.draw(func)
plotter.draw(prelim)
#for k in range (len(statLine)):
#	plotter.draw(statBox[k])
	
plotter.cd(2,1)
plotter.draw(avEvt)
plotter.draw(funcZoom)

plotter.update()

pb = BeautifierFactory.beautifyHPlotJa(plotter)
pad11 = pb.pad(1, 1)
labelX11 = pad11.axisLabel(0)
labelX11.centerLabel(0.0)
labelX11.changeFont(PlotFonts.AXIS_LABEL_FONT)
labelY11 = pad11.axisLabel(1)
labelY11.centerLabel(-0.005)
labelY11.changeFont(PlotFonts.AXIS_LABEL_FONT)

tickLabX11 = pad11.tickLabel(0)
tickLabX11.axisDistance(0.0)
tickLabX11.removeNegativeZero()
tickLabY11 = pad11.tickLabel(1)
tickLabY11.axisDistance(-0.00)
tickLabY11.displayAsPowerOfTen()

pad21 = pb.pad(2, 1)
labelX21 = pad21.axisLabel(0)
labelX21.centerLabel(0.0)
labelX21.changeFont(PlotFonts.AXIS_LABEL_FONT)
labelY21 = pad21.axisLabel(1)
labelY21.centerLabel(-0.01)
labelY21.changeFont(PlotFonts.AXIS_LABEL_FONT)

tickLabX21 = pad21.tickLabel(0)
tickLabX21.axisDistance(0.0)
tickLabX21.removeNegativeZero()
tickLabX21.displayAsDecimal("0.0")
tickLabY21 = pad21.tickLabel(1)
tickLabY21.axisDistance(-0.01)
tickLabY21.displayAsPowerOfTen()
       
plotter.update()
if writeFigures:
	for k in range(len(FileExtensions.FIG_TYPES)):
		outFileName = "BGO_AvPulse_Am241_4K"+ FileExtensions.FIG_TYPES[k]
		plotter.export(aidaPath + File.separator + outFileName)
		print("Writting File : " + aidaPath + File.separator + outFileName)

print "That's All Folks !!!\n"
if not showFigures:
	exit()	
